/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brucexx.wedding.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.brucexx.wedding.dao.PermissionDAO;
import com.brucexx.wedding.util.StringHelper;

/**
 * 
 * @author Administrator
 */
@Controller
public class AdminController {

	private static final Log logger = LogFactory.getLog(AdminController.class);

	@RequestMapping(value = "/manage.action")
	public String handleManage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		return "admin/no_permission";
	}

	@RequestMapping(value = "/admin.action", method = RequestMethod.GET)
	public String handleLoginGet(HttpServletRequest request,
			HttpServletResponse response) {
		String logout = request.getParameter("logout");
		if (!StringHelper.isEmpty(logout) && logout.equals("1")) {
			request.getSession().removeAttribute("username");
			return "index";
		}

		return "admin/admin";
	}

	@RequestMapping(value = "/admin.action", method = RequestMethod.POST)
	public String handleLoginPost(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// handle posts
		String ip = request.getRemoteHost();
		String name = request.getParameter("name").trim();
		logger.info("from ip[" + ip + "],name[" + name + "]");
		if (PermissionDAO.isAdmin(name)) {
			logger.info("from ip[" + ip + "],name[" + name
					+ "] enter into the admin ");
			request.getSession().setAttribute("username", name);
			return "admin/admin";
		}
		return "admin/no_permission";
	}

}
